// questionTypes.js - 题目类型服务模块
import api from './api'

export default {
  // 获取所有题目类型
  async fetchQuestionTypes() {
    try {
      const response = await api.getQuestionTypes()
      // console.log(`response.data.data:${JSON.stringify(response.data.data)}`)
      // console.log(`response.data:${JSON.stringify(response.data)}`)
      return response.data
    } catch (error) {
      throw new Error(`获取题目类型失败: ${error.response?.data?.message || error.message}`)
    }
  },
  
  // 创建题目类型
  async createQuestionType(typeData) {
    try {
      const response = await api.createQuestionType(typeData)
      return response.data
    } catch (error) {
      throw new Error(`创建失败: ${error.response?.data?.message || error.message}`)
    }
  },

  // 更新题目类型
  async updateQuestionType(id, updateData) {
    try {
      const response = await api.updateQuestionType(id, updateData)
      return response.data
    } catch (error) {
      throw new Error(`更新失败: ${error.response?.data?.message || error.message}`)
    }
  },
  
  // 删除题目类型
  async deleteQuestionType(id) {
    try {
      const response = await api.deleteQuestionType(id)
      return response.data
    } catch (error) {
      throw new Error(`删除失败: ${error.response?.data?.message || error.message}`)
    }
  },
  
  // 获取单个题目类型
  async fetchQuestionTypeById(id) {
    try {
      const response = await api.getQuestionTypeById(id)
      return response.data
    } catch (error) {
      throw new Error(`获取题目类型失败: ${error.response?.data?.message || error.message}`)
    }
  },
  
  // 搜索题目类型
  async searchQuestionTypes(query) {
    try {
      const response = await api.searchQuestionTypes(query)
      return response.data
    } catch (error) {
      throw new Error(`搜索失败: ${error.response?.data?.message || error.message}`)
    }
  },
  
  // 保存题型表单模板
  async saveFormTemplate(id, formTemplate) {
    try {
      const response = await api.updateQuestionType(id, { formTemplate })
      return response.data
    } catch (error) {
      throw new Error(`保存模板失败: ${error.response?.data?.message || error.message}`)
    }
  },
  
  // 获取题型表单模板
  async getFormTemplate(id) {
    try {
      const response = await api.getQuestionTypeById(id)
      return response.data.formTemplate
    } catch (error) {
      throw new Error(`获取模板失败: ${error.response?.data?.message || error.message}`)
    }
  }
}